package com.ruoyi.project.system.noticeRecord.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.noticeRecord.domain.NoticeRecord;
import com.ruoyi.project.system.noticeRecord.domain.NoticeRecordDTO;
import com.ruoyi.project.system.noticeRecord.service.INoticeRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 通知通告发送记录 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-07-23
 */
@Controller
@RequestMapping("/system/noticeRecord")
public class NoticeRecordController extends BaseController
{
    private String prefix = "system/noticeRecord";
	
	@Autowired
	private INoticeRecordService noticeRecordService;
	
	@RequiresPermissions("system:noticeRecord:view")
	@GetMapping()
	public String noticeRecord()
	{
	    return prefix + "/noticeRecord";
	}
	
	/**
	 * 新增通知通告发送记录
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存通知通告发送记录
	 */
	@RequiresPermissions("system:noticeRecord:add")
	@Log(title = "通知通告发送记录", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(NoticeRecord noticeRecord)
	{		
		return toAjax(noticeRecordService.insertNoticeRecord(noticeRecord));
	}

	/**
	 * 修改通知通告发送记录
	 */
	@GetMapping("/edit/{noticeRecordId}")
	public String edit(@PathVariable("noticeRecordId") Long noticeRecordId, ModelMap mmap)
	{
		NoticeRecord noticeRecord = noticeRecordService.selectNoticeRecordById(noticeRecordId);
		mmap.put("noticeRecord", noticeRecord);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存通知通告发送记录
	 */
	@RequiresPermissions("system:noticeRecord:edit")
	@Log(title = "通知通告发送记录", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(NoticeRecord noticeRecord)
	{		
		return toAjax(noticeRecordService.updateNoticeRecord(noticeRecord));
	}
	
	/**
	 * 删除通知通告发送记录
	 */
	@RequiresPermissions("system:noticeRecord:remove")
	@Log(title = "通知通告发送记录", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(noticeRecordService.deleteNoticeRecordByIds(ids));
	}
	/**
	 * 获取当前用户通知通告接收记录
	 */
	@ResponseBody
	@GetMapping("/message")
	public TableDataInfo message() {
		Map<String, Object> params = new HashMap<>(16);
		params.put("userId", ShiroUtils.getUserId());
		params.put("isRead","0");
		List<NoticeRecordDTO> dtoList = noticeRecordService.getNoticeRecordDTOList(params);
		for (NoticeRecordDTO noticeRecordDTO : dtoList) {
			noticeRecordDTO.setBefore(DateUtils.getTimeBefore(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",noticeRecordDTO.getCreateDateTimeStr())));
			noticeRecordDTO.setSender(noticeRecordDTO.getCreateBy());
		}
		return getDataTable(dtoList);
	}
	/**
	 * 获取通知通告接收记录列表
	 */
	@RequiresPermissions("system:noticeRecord:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(NoticeRecordDTO noticeParams) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("userId", ShiroUtils.getUserId());
		//String isread = noticeParams.getIsRead()==null?"0":noticeParams.getIsRead();
		params.put("isRead",noticeParams.getIsRead());
		params.put("noticeType",noticeParams.getNoticeType());
		params.put("noticeTitle",noticeParams.getNoticeTitle());
		List<NoticeRecordDTO> rows = noticeRecordService.getNoticeRecordDTOList(params);
		for (NoticeRecordDTO noticeDTO : rows) {
			noticeDTO.setBefore(DateUtils.getTimeBefore(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",noticeDTO.getCreateDateTimeStr())));
			noticeDTO.setSender(noticeDTO.getCreateBy());
		}
		return getDataTable(rows);
	}

	@Log(title = "通知通告发送记录", action = BusinessType.UPDATE)
	@PostMapping("/read")
	@ResponseBody
	public AjaxResult read(Long noticeRecordId)
	{
		NoticeRecord noticeRecord = noticeRecordService.selectNoticeRecordById(noticeRecordId);
		noticeRecord.setIsRead(1);
		noticeRecord.setReadDate(new Date());
		if (noticeRecordService.updateNoticeRecord(noticeRecord) > 0)
		{
			return success();
		}
		return error();
	}

	//@RequiresPermissions("system:noticeRecord:detail")
	@GetMapping("/detail/{noticeRecordId}")
	public String detail(@PathVariable("noticeRecordId") Long noticeRecordId, ModelMap mmap)
	{
		Map<String, Object> params = new HashMap<>(16);
		params.put("noticeRecordId", noticeRecordId);
		List<NoticeRecordDTO> rows = noticeRecordService.getNoticeRecordDTOList(params);
		mmap.put("NoticeRecordDTO", rows.get(0));
		return prefix + "/detail";
	}
}
